<?php

namespace application\diyform\controller;

use application\admin\controller\base as auth;
use ticky\request;
use ticky\response;
use ticky\schema;

//diyform/diyform_field

class diyform_field extends auth {

    private $modelid, $modeltable, $modelname;

    public function __construct() {
        parent::__construct();
        $this->modelid = isset($_GET['modelid']) ? intval($_GET['modelid']) : 1;
        $this->public_set_modelinfo();
    }

    /**
     * 字段列表
     */
    public function index() {
        $modelid = $this->modelid;
        $data = db('model_field')->where(array('modelid' => $modelid))->order('listorder ASC,field ASC')->select();
        $this->assign('items', $data);
        $this->assign('modelname', $this->modelname);
        $this->assign('modelname', $this->modelname);
        $this->assign('modelid', $this->modelid);
        $this->display('diyform_field_list');
    }

    public function manage() {
        $modelid = request::get('modelid', 0);
        $fieldid = request::get('fieldid', 0);
        $action = request::get('action', 'add');
        $item['modelid'] = $modelid;
        $item['fieldid'] = $fieldid;
        $item['fieldtype'] = '1';
        $item['dateset'] = '0';
        $item['minlength'] = '0';
        $item['maxlength'] = '100';
        $item['disabled'] = '1';
        if ($action == 'add' || $action == '添加') {
            $this->assign('postUrl', '/diyform/diyform_field/add');
            $this->assign('action', '添加');
        } else if ($action == 'edit' || $action == '修改') {
            $this->assign('postUrl', '/diyform/diyform_field/update');
            $this->assign('action', '修改');

            $item = db('model_field')->where(array('fieldid' => $fieldid))->find();
        }
        $this->assign('modelname', $this->modelname);
        $this->assign('modelid', $modelid);
        $this->assign('item', $item);
        $this->display('diyform_field_manage');
    }

    /**
     * 添加字段
     */
    public function add() {
        $data = $this->post_frm();
        if ($data['fieldtype'] == 'input' || $data['fieldtype'] == 'datetime') {
            schema::sql_add_field($this->modeltable, $data['field']);
        } else if ($data['fieldtype'] == 'textarea' || $data['fieldtype'] == 'images') {
            schema::sql_add_field_mediumtext($this->modeltable, $data['field']);
        } else if ($data['fieldtype'] == 'number') {
            schema::sql_add_field_int($this->modeltable, $data['field'], intval($data['defaultvalue']));
            $data['fieldtype'] = 'input';
        } else {
            schema::sql_add_field($this->modeltable, $data['field'], $data['defaultvalue'], $data['maxlength']);
        }
        db('model_field')->insert($data);
        showmsg('添加成功', '/diyform/diyform_field/?modelid=' . $this->modelid);
    }

    /**
     * 修改字段
     */
    public function update() {
        $model = db('model_field')->where(['fieldid' => request::post('fieldid', '0')])->find();
        $_POST['field'] = $model['field'];
        $_POST['fieldtype'] = $model['fieldtype'];
        $data = $this->post_frm();
        $fieldid = request::post('fieldid', '0');
        db('model_field')->where(['fieldid' => $fieldid])->update($data);
        showmsg('修改成功', '/diyform/diyform_field/?modelid=' . $this->modelid);
    }

    /**
     * 删除字段
     */
    public function delete() {
        $fieldid = request::get('fieldid', '0');
        $data = db('model_field')->field('field,issystem')->where(array('fieldid' => $fieldid))->find();
        if (!$data['issystem']) {
            db('model_field')->delete(['fieldid' => $fieldid]);
            schema::sql_del_field($this->modeltable, $data['field']);
            showmsg('删除成功', '/diyform/diyform');
        } else {
            showmsg('不能删除系统字段！');
        }
    }

    /**
     * 排序字段
     */
    public function order() {
        if (isset($_POST["dosubmit"])) {
            foreach ($_POST['fieldid'] as $key => $val) {
                db('model_field')->where(['fieldid' => intval($val)])->update(array('listorder' => $_POST['listorder'][$key]));
            }
            showmsg('排序字段成功！');
        }
    }

    /**
     * 获取模型信息
     */
    private function public_set_modelinfo() {
        $data = db('model')->field('name,tablename')->where(array('modelid' => $this->modelid))->find();
        if ($data) {
            $this->modelname = $data['name'];
            $this->modeltable = $data['tablename'];
        } else {
            showmsg('模型不存在！', 'stop');
        }
    }

    //表单数据
    private function post_frm() {
        $files = array('input', 'textarea', 'number', 'datetime', 'image', 'images', 'attachment', 'select', 'radio', 'checkbox');
        if (!in_array($_POST['fieldtype'], $files)) {
            showmsg('非法参数！', 'stop');
        }
        if (in_array($_POST['fieldtype'], array('select', 'radio', 'checkbox'))) {
            $_POST['setting'] = array2string(explode('|', rtrim($_POST['setting'], '|')));
        } elseif ($_POST['fieldtype'] == 'datetime') {
            $_POST['setting'] = $_POST['dateset'];
        } else {
            unset($_POST['setting']);
        }
        if ($_POST['minlength']) {
            $_POST['isrequired'] = 1;
        }
        $arr = array();
        $arr['modelid'] = $this->modelid;
        $arr['field'] = request::post('field', '');
        $arr['name'] = request::post('name', '');
        $arr['tips'] = request::post('tips', '');
        $arr['css'] = request::post('css', '');
        $arr['minlength'] = request::post('minlength', '0');
        $arr['maxlength'] = request::post('maxlength', '100');
        $arr['errortips'] = request::post('errortips', '');
        $arr['fieldtype'] = request::post('fieldtype', '');
        $arr['defaultvalue'] = request::post('defaultvalue', '');
        $arr['setting'] = request::post('setting', '');
        $arr['isrequired'] = request::post('isrequired', '1');
        $arr['issystem'] = request::post('issystem', '0');
        $arr['isunique'] = request::post('isunique', '');
        $arr['isadd'] = request::post('isadd', '');
        $arr['listorder'] = request::post('listorder', '1');
        $arr['disabled'] = request::post('disabled', '1');
        $arr['type'] = request::post('type', '');
        $arr['status'] = request::post('status', '0');
        return $arr;
    }

}
